package com.brillilab.starter.controller.aio.kit;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.kit.KitEnum.Classify;
import com.brillilab.domain.enums.lab.LabMemberPowerEnum;
import com.brillilab.domain.po.kit.Kit;
import com.brillilab.domain.po.kit.ReagentType;
import com.brillilab.domain.po.lab.LabMemberPower;
import com.brillilab.domain.vo.kit.ReagentTypeVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.kit.IKitService;
import com.brillilab.service.core.kit.IReagentTypeService;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;

/**
 * 试剂类型管理
 * 
 * @author lj
 *
 */
@RestController
@RequestMapping("/api/reagentType")
public class ReagentTypeController {
	@Autowired
	private IKitService kitService;
	@Autowired
	private IReagentTypeService reagentTypeService;
	@Autowired
	private UsersLogic usersLogic;

	/**
	 * 获取试剂类型
	 * 
	 * @param classify
	 *            分类：1-试剂，2-样品

	 * @return
	 */
	@RequestMapping(value = "/type", method = RequestMethod.GET)
	public ResponseVo getReagentType(@RequestHeader(name=StarterConstant.TOKEN) String token, 
			@RequestParam Integer classify) {
        UserInfoVo urs=usersLogic.getUserByToken(token);
		List<ReagentTypeVo> list = reagentTypeService.getTypeList(classify, urs.getLabId(), urs.getLabMemberId());
		return ResponseVo.success(list);
	}

	/**
	 * 添加试剂类型
	 * 
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ResponseVo addType(@RequestHeader(name=StarterConstant.TOKEN) String token,@RequestBody ReagentType reagentType) {
		
		Assert.isTrue((reagentType.getClassify()==1 || reagentType.getClassify()==2) 
				&& StringUtils.isNotBlank(reagentType.getName()) , "参数错误");	
		
		UserInfoVo urs=usersLogic.getUserByToken(token);
        reagentType.setLabId(urs.getLabId());
        reagentType.setLabMemberId(urs.getLabMemberId());
        ReagentType reagentType2 = reagentTypeService.insert(reagentType);
		return ResponseVo.success(reagentType2);
	}

	/**
	 * 修改试剂类型
	 * 
	 * @param reagentTypeId
	 *            试剂类型ID
	 * @param name
	 *            名称
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/modify", method = RequestMethod.GET)
	public ResponseVo modifyType(@RequestHeader(name=StarterConstant.TOKEN) String token, @RequestParam Long reagentTypeId, @RequestParam String name) {
		ReagentType reagentType = reagentTypeService.selectById(reagentTypeId);
		Assert.isTrue(reagentType.getTag()==1, "系统内置类型不允许修改");	

		UserInfoVo userVo=usersLogic.getUserByToken(token);
		Assert.isTrue(userVo.getLabId().equals(reagentType.getLabId()), "无权限修改该类型");	
		if(!userVo.getLabMemberId().equals(reagentType.getLabMemberId())) {
			Assert.isTrue(!CollectionUtils.isEmpty(userVo.getPowers()), "无权限修改该类型");
			List<String> powerList = userVo.getPowers().stream().map(LabMemberPower::getPower).collect(Collectors.toList());
			boolean contains = ArrayUtils.contains(powerList.toArray(), LabMemberPowerEnum.Power.REAGENT.getValue());
			Assert.isTrue(contains, "无权限修改该类型");
		}

		reagentType.setName(name);
		boolean b = reagentTypeService.updateById(reagentType);
		Assert.isTrue(b, "更新失败");	
		return ResponseVo.success();
	}

	/**
	 * 删除试剂类型
	 * 
	 * @param reagentTypeId
	 *            试剂类型ID
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public ResponseVo deleteType(@RequestHeader(name=StarterConstant.TOKEN) String token, @RequestParam Long reagentTypeId) {
		ReagentType reagentType = reagentTypeService.selectById(reagentTypeId);
		Assert.isTrue(reagentType.getTag()==1, "系统内置类型不允许删除");	
		
		UserInfoVo userVo=usersLogic.getUserByToken(token);
		Assert.isTrue(userVo.getLabId().equals(reagentType.getLabId()), "无权限删除该类型");	
		if(!userVo.getLabMemberId().equals(reagentType.getLabMemberId())) {
			Assert.isTrue(!CollectionUtils.isEmpty(userVo.getPowers()), "无权限删除该类型");
			List<String> powerList = userVo.getPowers().stream().map(LabMemberPower::getPower).collect(Collectors.toList());
			boolean contains = ArrayUtils.contains(powerList.toArray(), LabMemberPowerEnum.Power.REAGENT.getValue());
			Assert.isTrue(contains, "无权限删除该类型");
		}

		List<Kit> list = kitService.selectListByReagentType(reagentType.getLabId(), reagentTypeId);
		Assert.isTrue(list==null || list.size()==0, reagentType.getClassify().equals(Classify.SAMPLE.getValue())?"该类型下存在样品不允许删除":"该类型下存在试剂不允许删除");	
		boolean b = reagentTypeService.deleteById(reagentTypeId);
		Assert.isTrue(b, "删除失败");	
		return ResponseVo.success();
	}
	
	/**
	 * 是否隐藏试剂类型
	 * 
	 * @param reagentTypeId
	 *            试剂类型ID
	 * @param isHide 是否隐藏：0-否，1-是
	 * @return
	 */
	@RequestMapping(value = "/isHide", method = RequestMethod.GET)
	public ResponseVo isHideType(@RequestHeader(name=StarterConstant.TOKEN) String token, @RequestParam Long reagentTypeId, @RequestParam Integer isHide) {
		ReagentType reagentType = reagentTypeService.selectById(reagentTypeId);
		UserInfoVo urs=usersLogic.getUserByToken(token);
		Assert.isTrue(reagentType.getLabId()==null || urs.getLabId().equals(reagentType.getLabId()), "无权限隐藏该类型");
		boolean b = reagentTypeService.isHideType(urs.getLabId(), reagentTypeId, isHide, urs.getLabMemberId());
		Assert.isTrue(b, "操作失败");
		return ResponseVo.success();
	}

}
